package com.young.monitor.model;

import com.young.common.core.excel.ExcelColumn;
import com.young.common.util.JsonUtil;
import com.young.interfaces.log.model.LogDTO;

import java.util.Date;
/**
* 链路追踪实体类
* Created by imrookie on 2019-3-10.
*/
public class ApiTrace{

    @ExcelColumn(value = "主键", order = 1)
    private String id;//主键
    @ExcelColumn(value = "业务id", order = 2)
    private String businessId;//业务id
    @ExcelColumn(value = "链路追踪id", order = 3)
    private String traceId;//链路追踪id
    @ExcelColumn(value = "块id", order = 4)
    private String spanId;//块id
    @ExcelColumn(value = "父块id", order = 5)
    private String parentId;//父块id
    @ExcelColumn(value = "调用端发送调用请求的时间", order = 6)
    private Date cs;//调用端发送调用请求的时间
    @ExcelColumn(value = "服务端接收调用请求的时间", order = 7)
    private Date sr;//服务端接收调用请求的时间
    @ExcelColumn(value = "服务端发送响应的时间", order = 8)
    private Date ss;//服务端发送响应的时间
    @ExcelColumn(value = "调用端接收响应的时间", order = 9)
    private Date cr;//调用端接收响应的时间
    @ExcelColumn(value = "调用端ip", order = 10)
    private String cip;//调用端ip
    @ExcelColumn(value = "服务端ip", order = 11)
    private String sip;//服务端ip
    @ExcelColumn(value = "应用名", order = 12)
    private String appName;//应用名
    @ExcelColumn(value = "方法key", order = 13)
    private String key;//方法key
    private String keyInfo;//描述
    @ExcelColumn(value = "类名", order = 14)
    private String className;//类名
    @ExcelColumn(value = "方法名", order = 15)
    private String methodName;//方法名
    @ExcelColumn(value = "开始时间", order = 16)
    private Date beginTime;//开始时间
    @ExcelColumn(value = "结束时间", order = 17)
    private Date endTime;//结束时间
    @ExcelColumn(value = "耗时", order = 18)
    private Integer ms;//耗时
    @ExcelColumn(value = "服务器IP", order = 19)
    private String serverIp;//服务器IP
    @ExcelColumn(value = "是否发生异常", order = 20)
    private Integer hasEx;//是否发生异常
    @ExcelColumn(value = "异常信息", order = 21)
    private String ex;//异常信息
    @ExcelColumn(value = "入参", order = 22)
    private String inParam;//入参
    @ExcelColumn(value = "出参", order = 23)
    private String outParam;//出参
    @ExcelColumn(value = "用户id", order = 24)
    private String userId;//用户id
    @ExcelColumn(value = "用户姓名", order = 25)
    private String userName;//用户姓名
    @ExcelColumn(value = "用户ip", order = 26)
    private String userIp;//用户ip
    @ExcelColumn(value = "操作系统", order = 27)
    private String os;//操作系统
    @ExcelColumn(value = "浏览器", order = 28)
    private String browser;//浏览器
    @ExcelColumn(value = "请求的协议,http/https等", order = 29)
    private String scheme;//请求的协议,http/https等
    @ExcelColumn(value = "请求的host", order = 30)
    private String host;//请求的host
    @ExcelColumn(value = "请求的端口号", order = 31)
    private Integer port;//请求的端口号
    @ExcelColumn(value = "请求的uri", order = 32)
    private String uri;//请求的uri
    @ExcelColumn(value = "请求的url", order = 33)
    private String url;//请求的url
    @ExcelColumn(value = "请求方式,post/get等", order = 34)
    private String method;//请求方式,post/get等
    @ExcelColumn(value = "请求头中的referer", order = 35)
    private String referer;//请求头中的referer
    @ExcelColumn(value = "请求头中的origin", order = 36)
    private String origin;//请求头中的origin
    @ExcelColumn(value = "扩展参数", order = 37)
    private String extParam;//扩展参数

    private String orderBy;//排序字段
    private String orderType;//排序类型,升序asc/降序desc

    /**
     * logdto转apitrace对象
     * @param logDTO
     * @return
     */
    public static final ApiTrace newInstanceFromLogDTO(LogDTO logDTO) {
        ApiTrace apiTrace = new ApiTrace();
        if (logDTO == null) {
            return apiTrace;
        }
        apiTrace.setAppName(logDTO.getAppName());
        apiTrace.setKey(logDTO.getKey());
        apiTrace.setKeyInfo(logDTO.getLogContent());
        apiTrace.setClassName(logDTO.getClassName());
        apiTrace.setMethodName(logDTO.getMethodName());
        apiTrace.setBusinessId(logDTO.getBusinessId());

        apiTrace.setTraceId(logDTO.getTraceId());
        apiTrace.setSpanId(logDTO.getSpanId());
        apiTrace.setParentId(logDTO.getParentId());
        apiTrace.setCs(logDTO.getCs());
        apiTrace.setSr(logDTO.getSr());
        apiTrace.setSs(logDTO.getSs());
        apiTrace.setCr(logDTO.getCr());
        apiTrace.setCip(logDTO.getCip());
        apiTrace.setSip(logDTO.getSip());

        apiTrace.setBeginTime(logDTO.getBeginTime());
        apiTrace.setEndTime(logDTO.getEndTime());
        apiTrace.setMs((int)logDTO.getMs());
        apiTrace.setServerIp(logDTO.getServerIp());
        apiTrace.setHasEx(logDTO.getException() != null ? 1 : 0);
        apiTrace.setEx(logDTO.getException() != null ? logDTO.getException().toString() : null);
        apiTrace.setInParam(JsonUtil.toJson(logDTO.getParams()));
        apiTrace.setOutParam(JsonUtil.toJson(logDTO.getResult()));

        if (logDTO.getLoginUser() != null) {
            apiTrace.setUserId(logDTO.getLoginUser().getId());
            apiTrace.setUserName(logDTO.getLoginUser().getName());
        }
        apiTrace.setUserIp(logDTO.getClientIp());
        apiTrace.setOs(logDTO.getOs());
        apiTrace.setBrowser(logDTO.getBrowser());
        apiTrace.setScheme(logDTO.getScheme());
        apiTrace.setHost(logDTO.getHost());
        apiTrace.setPort(logDTO.getPort());
        apiTrace.setUri(logDTO.getUri());
        apiTrace.setUrl(logDTO.getUrl());
        apiTrace.setMethod(logDTO.getMethod());
        apiTrace.setReferer(logDTO.getReferer());
        apiTrace.setOrigin(logDTO.getOrigin());

        apiTrace.setExtParam(JsonUtil.toJson(logDTO.getExtParam()));
        return apiTrace;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("{");
        sb.append("\"id\":").append(id != null ? "\"" : "").append(id).append(id != null ? "\"" : "");
        sb.append(", \"appName\":").append(appName != null ? "\"" : "").append(appName).append(appName != null ? "\"" : "");
        sb.append(", \"key\":").append(key != null ? "\"" : "").append(key).append(key != null ? "\"" : "");
        sb.append(", \"keyInfo\":").append(keyInfo != null ? "\"" : "").append(keyInfo).append(keyInfo != null ? "\"" : "");
        sb.append(", \"className\":").append(className != null ? "\"" : "").append(className).append(className != null ? "\"" : "");
        sb.append(", \"methodName\":").append(methodName != null ? "\"" : "").append(methodName).append(methodName != null ? "\"" : "");
        sb.append(", \"businessId\":").append(businessId != null ? "\"" : "").append(businessId).append(businessId != null ? "\"" : "");

        sb.append(", \"traceId\":").append(traceId != null ? "\"" : "").append(traceId).append(traceId != null ? "\"" : "");
        sb.append(", \"spanId\":").append(spanId != null ? "\"" : "").append(spanId).append(spanId != null ? "\"" : "");
        sb.append(", \"parentId\":").append(parentId != null ? "\"" : "").append(parentId).append(parentId != null ? "\"" : "");
        sb.append(", \"cs\":").append(cs != null ? "\"" : "").append(cs).append(cs != null ? "\"" : "");
        sb.append(", \"sr\":").append(sr != null ? "\"" : "").append(sr).append(sr != null ? "\"" : "");
        sb.append(", \"ss\":").append(ss != null ? "\"" : "").append(ss).append(ss != null ? "\"" : "");
        sb.append(", \"cr\":").append(cr != null ? "\"" : "").append(cr).append(cr != null ? "\"" : "");
        sb.append(", \"cip\":").append(cip != null ? "\"" : "").append(cip).append(cip != null ? "\"" : "");
        sb.append(", \"sip\":").append(sip != null ? "\"" : "").append(sip).append(sip != null ? "\"" : "");

        sb.append(", \"beginTime\":").append(beginTime != null ? "\"" : "").append(beginTime).append(beginTime != null ? "\"" : "");
        sb.append(", \"endTime\":").append(endTime != null ? "\"" : "").append(endTime).append(endTime != null ? "\"" : "");
        sb.append(", \"ms\":").append(ms);
        sb.append(", \"serverIp\":").append(serverIp != null ? "\"" : "").append(serverIp).append(serverIp != null ? "\"" : "");
        sb.append(", \"hasEx\":").append(hasEx);
        sb.append(", \"ex\":").append(ex != null ? "\"" : "").append(ex).append(ex != null ? "\"" : "");
        sb.append(", \"inParam\":").append(inParam != null ? "\"" : "").append(inParam).append(inParam != null ? "\"" : "");
        sb.append(", \"outParam\":").append(outParam != null ? "\"" : "").append(outParam).append(outParam != null ? "\"" : "");

        sb.append(", \"userId\":").append(userId != null ? "\"" : "").append(userId).append(userId != null ? "\"" : "");
        sb.append(", \"userName\":").append(userName != null ? "\"" : "").append(userName).append(userName != null ? "\"" : "");
        sb.append(", \"userIp\":").append(userIp != null ? "\"" : "").append(userIp).append(userIp != null ? "\"" : "");
        sb.append(", \"os\":").append(os != null ? "\"" : "").append(os).append(os != null ? "\"" : "");
        sb.append(", \"browser\":").append(browser != null ? "\"" : "").append(browser).append(browser != null ? "\"" : "");
        sb.append(", \"scheme\":").append(scheme != null ? "\"" : "").append(scheme).append(scheme != null ? "\"" : "");
        sb.append(", \"host\":").append(host != null ? "\"" : "").append(host).append(host != null ? "\"" : "");
        sb.append(", \"port\":").append(port);
        sb.append(", \"uri\":").append(uri != null ? "\"" : "").append(uri).append(uri != null ? "\"" : "");
        sb.append(", \"url\":").append(url != null ? "\"" : "").append(url).append(url != null ? "\"" : "");
        sb.append(", \"method\":").append(method != null ? "\"" : "").append(method).append(method != null ? "\"" : "");
        sb.append(", \"referer\":").append(referer != null ? "\"" : "").append(referer).append(referer != null ? "\"" : "");
        sb.append(", \"origin\":").append(origin != null ? "\"" : "").append(origin).append(origin != null ? "\"" : "");

        sb.append(", \"extParam\":").append(extParam != null ? "\"" : "").append(extParam).append(extParam != null ? "\"" : "");
        sb.append('}');
        return sb.toString();
    }

    public ApiTrace(){
        super();
    }

    public String getKeyInfo() {
        return keyInfo;
    }

    public void setKeyInfo(String keyInfo) {
        this.keyInfo = keyInfo;
    }

    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getBusinessId() {
    return businessId;
    }
    public void setBusinessId(String businessId) {
    this.businessId = businessId;
    }
    public String getTraceId() {
    return traceId;
    }
    public void setTraceId(String traceId) {
    this.traceId = traceId;
    }
    public String getSpanId() {
    return spanId;
    }
    public void setSpanId(String spanId) {
    this.spanId = spanId;
    }
    public String getParentId() {
    return parentId;
    }
    public void setParentId(String parentId) {
    this.parentId = parentId;
    }
    public Date getCs() {
    return cs;
    }
    public void setCs(Date cs) {
    this.cs = cs;
    }
    public Date getSr() {
    return sr;
    }
    public void setSr(Date sr) {
    this.sr = sr;
    }
    public Date getSs() {
    return ss;
    }
    public void setSs(Date ss) {
    this.ss = ss;
    }
    public Date getCr() {
    return cr;
    }
    public void setCr(Date cr) {
    this.cr = cr;
    }
    public String getCip() {
    return cip;
    }
    public void setCip(String cip) {
    this.cip = cip;
    }
    public String getSip() {
    return sip;
    }
    public void setSip(String sip) {
    this.sip = sip;
    }
    public String getAppName() {
    return appName;
    }
    public void setAppName(String appName) {
    this.appName = appName;
    }
    public String getKey() {
    return key;
    }
    public void setKey(String key) {
    this.key = key;
    }
    public String getClassName() {
    return className;
    }
    public void setClassName(String className) {
    this.className = className;
    }
    public String getMethodName() {
    return methodName;
    }
    public void setMethodName(String methodName) {
    this.methodName = methodName;
    }
    public Date getBeginTime() {
    return beginTime;
    }
    public void setBeginTime(Date beginTime) {
    this.beginTime = beginTime;
    }
    public Date getEndTime() {
    return endTime;
    }
    public void setEndTime(Date endTime) {
    this.endTime = endTime;
    }
    public Integer getMs() {
    return ms;
    }
    public void setMs(Integer ms) {
    this.ms = ms;
    }
    public String getServerIp() {
    return serverIp;
    }
    public void setServerIp(String serverIp) {
    this.serverIp = serverIp;
    }
    public Integer getHasEx() {
    return hasEx;
    }
    public void setHasEx(Integer hasEx) {
    this.hasEx = hasEx;
    }
    public String getEx() {
    return ex;
    }
    public void setEx(String ex) {
    this.ex = ex;
    }
    public String getInParam() {
    return inParam;
    }
    public void setInParam(String inParam) {
    this.inParam = inParam;
    }
    public String getOutParam() {
    return outParam;
    }
    public void setOutParam(String outParam) {
    this.outParam = outParam;
    }
    public String getUserId() {
    return userId;
    }
    public void setUserId(String userId) {
    this.userId = userId;
    }
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public String getUserIp() {
    return userIp;
    }
    public void setUserIp(String userIp) {
    this.userIp = userIp;
    }
    public String getOs() {
    return os;
    }
    public void setOs(String os) {
    this.os = os;
    }
    public String getBrowser() {
    return browser;
    }
    public void setBrowser(String browser) {
    this.browser = browser;
    }
    public String getScheme() {
    return scheme;
    }
    public void setScheme(String scheme) {
    this.scheme = scheme;
    }
    public String getHost() {
    return host;
    }
    public void setHost(String host) {
    this.host = host;
    }
    public Integer getPort() {
    return port;
    }
    public void setPort(Integer port) {
    this.port = port;
    }
    public String getUri() {
    return uri;
    }
    public void setUri(String uri) {
    this.uri = uri;
    }
    public String getUrl() {
    return url;
    }
    public void setUrl(String url) {
    this.url = url;
    }
    public String getMethod() {
    return method;
    }
    public void setMethod(String method) {
    this.method = method;
    }
    public String getReferer() {
    return referer;
    }
    public void setReferer(String referer) {
    this.referer = referer;
    }
    public String getOrigin() {
    return origin;
    }
    public void setOrigin(String origin) {
    this.origin = origin;
    }
    public String getExtParam() {
    return extParam;
    }
    public void setExtParam(String extParam) {
    this.extParam = extParam;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

}